package com.platform.core.web.controller;

import cn.hutool.core.collection.CollUtil;
import com.platform.aspect.WebLog;
import com.platform.comm.validation.groups.Insert;
import com.platform.comm.validation.groups.Modify;
import com.platform.core.service.SysRoleService;
import com.platform.dto.RoleQo;
import com.querydsl.core.BooleanBuilder;
import com.platform.comm.web.results.DataJsonResult;
import com.platform.comm.web.results.JsonResult;
import com.platform.comm.web.results.PageListJsonResult;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Description: 角色管理
 *
 * @author libin
 * Created on 2019/4/23
 **/
@RestController
@RequestMapping("management/role")
public class SysRoleController extends BaseController {

    private final SysRoleService sysRoleService;

    public SysRoleController(SysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    @WebLog(theme = "新增[角色]", moduleName = "角色管理", type = WebLog.OpType.ADD, root = RoleQo.class)
    @PostMapping(name = "新增角色", path = "create")
    public JsonResult create(@Validated({Insert.class}) @RequestBody RoleQo roleQo) {
        return createOrModifyWithResult(sysRoleService::create, roleQo);
    }

    @WebLog(theme = "更新[角色]", moduleName = "角色管理", type = WebLog.OpType.MOD, root = RoleQo.class)
    @PostMapping(name = "更新角色", path = "modify")
    public JsonResult modify(@Validated({Modify.class}) @RequestBody RoleQo roleQo) {
        return createOrModifyWithResult(sysRoleService::modify, roleQo);
    }

    @WebLog(theme = "删除[角色]", moduleName = "角色管理", type = WebLog.OpType.DEL, root = RoleQo.class)
    @PostMapping(name = "删除角色", path = "remove")
    public JsonResult remove(@RequestBody RoleQo roleQo) {
        return removeOrOtherByIds(sysRoleService::remove, roleQo.getId());
    }

    @WebLog(theme = "删除[批量删除角色]", moduleName = "角色管理", type = WebLog.OpType.DEL_BATCH, root = List.class)
    @PostMapping(name = "批量删除角色", path = "batchRemove")
    public JsonResult batchRemove(@RequestBody List<String> ids) {
        return removeOrOtherByIds(sysRoleService::batchRemove, ids);
    }

    @PostMapping(name = "分页查询角色", path = "search")
    public PageListJsonResult<RoleQo> search(@RequestBody Map<String, String> parametersMap) {
        MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
        if (CollUtil.isNotEmpty(parametersMap)) {
            parametersMap.forEach(parameters::add);
        }
        return queryByPaging(sysRoleService::queryRoles, new BooleanBuilder(), getPageable(parametersMap), parameters);
    }

    @PostMapping(name = "检查角色编码是否存在", path = "checkCodeExists")
    public DataJsonResult<Boolean> checkCodeExists(@RequestBody Map<String, String> parametersMap) {
        String id = parametersMap.get("id");
        String roleCode = parametersMap.get("roleCode");
        return query(sysRoleService::checkCodeExists, id, roleCode);
    }

    @PostMapping(name = "检查角色名是否存在", path = "checkNameExists")
    public DataJsonResult<Boolean> checkNameExists(@RequestBody Map<String, String> parametersMap) {
        String id = parametersMap.get("id");
        String roleName = parametersMap.get("roleName");
        return query(sysRoleService::checkNameExists, id, roleName);
    }


    @PostMapping(name = "查询当前用户所拥有的角色", path = "roles")
    public Set<String> roles() {
        return sysRoleService.roles();
    }

    @PostMapping(name = "同步角色到子系统", path = "syncSubsystem")
    public JsonResult syncSubsystem(@RequestBody Map<String, String> parametersMap) {
        String id = parametersMap.get("id");
        return removeOrOtherByIds(sysRoleService::syncSubsystem, id);
    }

    @PostMapping(name = "根据前缀查询角色", path = "searchByPrefix")
    public List<RoleQo> queryRoles(@RequestBody Map<String, String> parametersMap){
        return this.sysRoleService.queryRoleByPrefix(parametersMap);
    }


    @PostMapping(name = "根据id获取角色信息", path = "queryRoleById")
    public RoleQo getByRoleId(String id) {
        return this.sysRoleService.getByRoleId(id);
    }

    @PostMapping(name = "根据ids获取角色信息列表", path = "queryRoleByIds")
    public List<RoleQo> getByRoleIds(@RequestBody List<String> roleIds) {
        return this.sysRoleService.getByRoleId(roleIds);
    }
}
